home *** CD-ROM | disk | FTP | other *** search
- /*
- **
- ** ParseFullPathname
- **
- ** Takes a *complete* full pathname and turns it into a DirID and VRefNum pair.
- ** This does not handle partial pathnames because it assumes that the first element
- ** in the pathname is volume name.
- **
- ** to recurse or not to recurse, that is the question... (not)
- **
- **
- ** Neil Day
- ** MacDTS
- ** Apple Computer, Inc.
- **
- */
-
- #include <stdio.h> // Compiler Interfaces
- #include <files.h>
- #include <String.h>
- #include <Strings.h>
- #include <Memory.h>
- #include <Errors.h>
-
- OSErr ParseFullPathname (FSSpec *Result, char *PathNamePtr); // function prototypes
- Boolean GetElement (StringPtr Result,char * PathNamePtr,short ElementNumber);
-
- main (int argc,char ** argv)
- {
- OSErr err;
- FSSpec here;
-
- if (argc != 2)
- printf ("form: pfpn <pathname>\n");
-
- err = ParseFullPathname (&here, argv[1]);
- if (err)
- printf ("Died of error %d\n",err);
- else
- printf ("vRefNum %d DirID %d name %P\n",here.vRefNum, here.parID,here.name);
- }
-
- Boolean GetElement (StringPtr Result,char *PathNamePtr,short ElementNumber)
- {
- char *eStart, *eEnd;
-
- eStart = eEnd = PathNamePtr;
-
- while (ElementNumber) { // Search for the element
- if (*eEnd == ':' || !(*eEnd)) { // if we see colon or a null , we're at the end of element
- --ElementNumber; // one down, n-1 to go
- if (ElementNumber == 1) // are we at the second to last element??
- eStart = eEnd + 1; // mark it.
- }
- if (!(*eEnd)) break;
- ++eEnd; // always increment
- }
-
- if (ElementNumber || (eEnd - eStart > 32) || (eEnd - eStart == 0)) // If n > 0 or the element is too big or there is no element
- return false; // then croak.
-
- BlockMove ((Ptr) eStart, (Ptr) (Result + 1), // Move the substring into the Result
- *Result = (char)eEnd - eStart);
-
- return true;
- }
-
-
- OSErr ParseFullPathname (FSSpec *Result,char *PathNamePtr)
- {
- OSErr err;
- short level = 2;
- CInfoPBRec dInfo;
- HParamBlockRec vInfo;
-
- Result->parID = fsRtDirID; // start at the top of the volume
-
- if (GetElement ((StringPtr) &Result->name, PathNamePtr, 1)) { // If there is a volume name
- --(*Result->name);
- vInfo.volumeParam.ioVRefNum = 0; // Get its vRefNum...
- vInfo.volumeParam.ioNamePtr = &Result->name;
- vInfo.volumeParam.ioVolIndex = -1;
- err = PBHGetVInfo (&vInfo,false);
- printf ("err %d\n",err);
- if (err) return err; // and return an error if we couldn't find it
- Result->vRefNum = vInfo.volumeParam.ioVRefNum; // ...or put it into the Result
- }
-
- while (GetElement ((StringPtr) &Result->name, PathNamePtr, level)) { // Walk down the pathname
- printf ("%P, %d, %c\n",Result->name, *Result->name, (Result->name + (*Result->name) -1 ));
- if ((Result->name + (*Result->name-1)) == ':' ) { // if the last char is a colon
- --*(Result->name); // get rid of the colon on the end of the element
- printf ("%P\n",Result->name);
- }
- dInfo.dirInfo.ioFDirIndex = 0; // and get it's DirID
- dInfo.dirInfo.ioDrDirID = Result->parID;
- dInfo.dirInfo.ioVRefNum = Result->vRefNum;
- dInfo.dirInfo.ioNamePtr = &Result->name;
- err = PBGetCatInfo (&dInfo,false);
- if (err) return err; // if there was an error, return it
- Result->parID = dInfo.dirInfo.ioDrDirID; // otherwise, update the current DirID
- ++level; // go on to the next level
- }
-
- printf ("what's left over: %s\n",PathNamePtr);
-
-
- return noErr; // no problems
- }
-